package com.线程通知;

import lombok.SneakyThrows;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.ServiceLoader;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: LCG
 * @date: 2022-06-20 14:54:41
 * @description: 用来测试线程的通知
 **/
public class TestThreadTongZhi {
    public static void main(String[] args) {

        ReentrantLock reentrantLock=new ReentrantLock();
        Condition condition1 = reentrantLock.newCondition();
        Condition condition2 = reentrantLock.newCondition();


        Thread t1=new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                for (int i=1;i<=10;i++){
                    Thread.sleep(400);
                    reentrantLock.lock();
                    if(i%2!=0){
                        System.out.println("A---->"+i);
                    }
                    condition1.await();
                    System.out.println("=================A1");
                    reentrantLock.unlock();

                }
            }
        },"A");


        Thread t2=new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                for (int i=1;i<=10;i++){
                    Thread.sleep(500);
                    reentrantLock.lock();
                    if(i%2==0){
                        System.out.println("B---->"+i);
                    }
                    condition1.signal();
                    reentrantLock.unlock();

                }
            }
        },"B");


        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("222");
        arrayList.add("123");
        arrayList.add("222");
        arrayList.add("234");
        arrayList.add("222");



        t1.start();
        t2.start();

    }
}
